<?php

namespace App\Services\ApplyGoldRecordBatchPay;

use App\Models\User;
use App\Jobs\ApplyGoldProductPayingJob;


class ApplyGoldRecordBatchPay
{
	protected User $user;

	protected array $unpaidCacheIds = [];

	public function __construct(User $user, $unpaidCacheIds,$method)
	{
		$this->user = $user;
		$this->unpaidCacheIds = $unpaidCacheIds;
		$this->method = $method;
	}

	public function pay()
	{
		if (count($this->unpaidCacheIds) <= 0) return;

		if($this->method == 'balance'){
			foreach ($this->unpaidCacheIds as $cacheId) {
	            $this->user->markRecordPayCachePaying($cacheId);
	            $this->user->addRecordToPendingCache($cacheId);
				$this->user->delRecordFromUnpaidCache($cacheId);
	            $queue = app()->environment('local') ? 'default' : 'default';
	            dispatch(new ApplyGoldProductPayingJob($this->user, $cacheId))->onQueue($queue);
			}

		}elseif($this->method =="alipay"){

			return cashier($this->method)->payStorm($this->unpaidCacheIds);
		}elseif($this->method =="wxpay"){

			return cashier($this->method)->payStorm($this->unpaidCacheIds);
		}
		
	}
}
